<template>
	<view class="container">
		<view class="mu-list">
			<template v-for="item in callLog">
				<view class="mu-list-item">
					<view class="mu-list-item-number">{{item.number}}</view>
					<view class="mu-list-item-time">
						<text>拨号时间：</text>
						<!-- {{new Date(item.talkTime).Format('yyyy-MM-dd hh:mm:ss')}} -->
						{{$thirteenBitTimestamp(item.talkTime)}}
					</view>
					<view class="mu-list-item-time">
						<text>结束时间：</text>
						<!-- {{new Date(item.lastModified).Format('yyyy-MM-dd hh:mm:ss')}} -->
						{{$thirteenBitTimestamp(item.lastModified)}}
					</view>
					<view v-if="item.recordFile" class="mu-list-item-record" @click="playRecord(item.recordFile)">
						<text>录音文件：</text>
						{{item.recordFile.name}}
					</view>
					<view class="mu-list-item-sim">
						<text>SIM卡信息：</text>
						{{JSON.stringify(item.simInfo)}}
					</view>
				</view>
			</template>
		</view>
	</view>
</template>

<script>
	// #ifdef APP-PLUS
	const muCall = uni.requireNativePlugin('Mu-Call');
	// #endif

	var innerAudioContext;

	export default {
		data() {
			return {
				callLog: []
			}
		},
		watch: {
			callLog: {
				handler(newValue, oldValue) {
					if (Array.isArray(newValue) && newValue.length > 0) {

						this.uploadList(newValue)
						this.UploadCall(newValue)
					} else {
						console.error('newValue is not an array or is empty');
					}
				},
				deep: true // 深度监听数组内部的变化  
			}
		},
		onLoad() {
			// #ifdef APP-PLUS

			const that = this
			//不限时间范围
			muCall.getCallLogList(res => {
				that.callLog = res;
			})

			//以下是有选择时间范围的
			// muCall.getCallLogList(res => {
			// 	that.callLog = res;

			// }, {
			// 	startTime: 1705893735000,
			// 	endTime: 1708308069000
			// })
			// #endif
		},
		onHide() {
			if (innerAudioContext) {
				try {
					innerAudioContext.pause();
					innerAudioContext.destroy()
					innerAudioContext = null
				} catch (e) {
					console.log(e)
				}
			}
		},
		methods: {
			UploadCall(newValue) { //上传通话记录
				let that = this;
				newValue.forEach(item => {
					if (item && item.recordFile) {
						this.request('/outcalllog/outCallLog/add', 'post', {
							dialingTime: item.talkTime, //拨号时间
							dialEndingTime: item.lastModified, //拨号结束时间
							makingPhoneCalls: item.number, //拨打电话
							nativePhone: item.simInfo.number, //本机号码
						}).then(res => {
							if (res.data.code != 200) {
								uni.showToast({
									title: res.data.msg,
									icon: 'none'
								})
							} else {

							}
						})
					}
				})
			},
			uploadList(newValue) { //上传音频
				const that = this
				const api = 'http://192.168.1.16:8080/jeecg-boot/sys/common/uploadLocalAly';
				// 遍历newValue数组中的每个文件项  
				newValue.forEach((item, index) => {
					// 检查item是否有recordFile属性  
					if (item && item.recordFile) {
						muCall.uploadFile({
								url: api,
								filePath: item.recordFile.path,
								name: "file",
								header: {
									// 请求头
								},
								formData: {
									biz: "scott/pic",
									file: item.recordFile.path
									// 请求表单数据
								},
								timeout: 60000 // 响应超时时间, 单位ms
							}, res => {
								const result = JSON.parse(res.data)
								let {
									code, // 响应码
									message, // 响应消息
									success, // 接口响应数据
									result: resultinfo //返回结果重命名
								} = result

							}

						)
					} else {

					}
				});
			},

			playRecord(recordFile) {
				if (innerAudioContext) {
					try {
						innerAudioContext.pause();
						innerAudioContext.destroy()
						innerAudioContext = null
					} catch (e) {
						console.log(e)
					}
				}

				let {
					path
				} = recordFile;
				innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = false;
				innerAudioContext.src = path;
				innerAudioContext.onPlay(() => {
					console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
					console.log(res.errMsg);
					console.log(res.errCode);
				});
				innerAudioContext.play();
			}
		}
	}
</script>

<style>
	page {
		background-color: #F8F8F8;
	}

	.container {
		padding: 30rpx;
		box-sizing: border-box;
	}

	.mu-list {
		background: #fff;
		border-radius: 10rpx;
		padding: 10rpx 0;
		box-sizing: border-box;
	}

	.mu-list .mu-list-item {
		padding: 20rpx;
		box-sizing: border-box;
		border-bottom: 1px solid #EBEBEB;
	}

	.mu-list .mu-list-item:last-child {
		border-bottom: none;
	}

	.mu-list-item .mu-list-item-number {
		font-size: 32rpx;
		color: #333333;
		font-weight: bold;
	}

	.mu-list-item .mu-list-item-time {
		font-size: 28rpx;
		color: #999999;
		margin-top: 10rpx;
		font-weight: 400;
	}

	.mu-list-item-time text {
		color: #333333;
	}

	.mu-list-item .mu-list-item-record {
		font-size: 28rpx;
		color: #007aff;
		margin-top: 10rpx;
		font-weight: 400;
		white-space: pre-wrap;
		word-break: break-all;
	}

	.mu-list-item-record text {
		color: #333333;
	}

	.mu-list-item .mu-list-item-sim {
		font-size: 28rpx;
		color: #999999;
		margin-top: 10rpx;
		font-weight: 400;
		white-space: pre-wrap;
		word-break: break-all;
	}

	.mu-list-item-sim text {
		color: #333333;
	}
</style>